import pymongo

from settings import (
  MONGO_DB_NAME,
  MONGO_DB_URL,
  COLLECTION_MOVIE_INFO,
  COLLECTION_MOVIE_CREDITS,
  COLLECTION_MOVIE_KEYWORDS,
)
# 连接到MongoDB服务器
def connect():
  client = pymongo.MongoClient(MONGO_DB_URL)
  # 选择数据库（如果不存在，会自动创建）  
  return client

def use_db(db_name):
  client = connect()
  db = client[db_name]
  return db

def use_collection(collection_name):
  db = use_db(MONGO_DB_NAME)
  collection = db[collection_name]
  return collection

def insert_data(collection, dict_data):
  collection.insert_one(dict_data)

def get_collection_data_length(collection):
  return collection.count_documents({})

# 按顺序获取collection的数据
def get_all_data(collection):
  # 获取所有数据
  cursor = collection.find()
  # 按顺序遍历并打印数据
  for document in cursor:
    print(document)
    return

collection_movie_info = use_collection(COLLECTION_MOVIE_INFO)
collection_movie_credits = use_collection(COLLECTION_MOVIE_CREDITS)
collection_movie_keywords = use_collection(COLLECTION_MOVIE_KEYWORDS)

# print(get_data_length(collection_movie_info))